#!/bin/sh
# 编译安装管理Memcached
App=memcached
AppName=Memcached
AppBase=/App
AppDir=$AppBase/$App
AppSrcBase=/App/src
AppSrcFile=$App-*.tar.*
AppSrcDir=$(find $AppSrcBase -maxdepth 1 -name "$AppSrcFile" -type f | sed -e 's/.tar.*$//' -e 's/^.\///')

AppUser=$App
AppGroup=$AppUser
AppProg=$AppDir/bin/$App
Port=11211
MaxConn=10240
MaxMem=4096
MaxThreads=8
MaxBacklog=5120
Options="-u $AppUser -p $Port -c $MaxConn -m $MaxMem -t $MaxThreads -b $MaxBacklog -d"

RemoveFlag=0
InstallFlag=0

# 获取PID
fpid()
{
    AppPid=$(ps aux | grep "$AppProg" | grep -v "grep" | awk '{print $2}' 2> /dev/null)
}

# 查询状态
fstatus()
{
    fpid

    if [ ! -f "$AppProg" ]; then
        echo "$AppName 未安装"
    else
        echo "$AppName 已安装"
        if [ -z "$AppPid" ]; then
            echo "$AppName 未启动"
        else
            echo "$AppName 正在运行"
        fi
    fi
}

# 删除
fremove()
{
    fpid
    RemoveFlag=1

    if [ -z "$AppPid" ]; then
        if [ -d "$AppDir" ]; then
            rm -rf $AppDir && echo "删除 $AppName"
        else
            echo "$AppName 未安装"
        fi
    else
        echo "$AppName 正在运行" && exit 1
    fi

}

# 备份
fbackup()
{
    Day=$(date +%Y-%m-%d)
    BackupFile=$App-$Day.tgz

    if [ -f "$AppProg" ]; then
        cd $AppBase
        tar zcvf $BackupFile $App --backup=numbered
        [ $? -eq 0 ] && echo "$AppName 备份成功" || echo "$AppName 备份失败"
    else
        echo "$AppName 未安装"
    fi
}

# 安装
finstall()
{
    fpid
    InstallFlag=1

    if [ -z "$AppPid" ]; then
        test -f "$AppProg" && echo "$AppName 已安装"
        [ $? -ne 0 ] && fupdate
    else
        echo "$AppName 正在运行"
    fi
}

# 更新
fupdate()
{
    Operate="更新"
    [ $InstallFlag -eq 1 ] && Operate="安装"
    [ $RemoveFlag -ne 1 ] && fbackup

    cd $AppSrcBase
    test -d $AppSrcDir && rm -rf $AppSrcDir

    tar zxf $AppSrcFile || tar jxf $AppSrcFile
    cd $AppSrcDir
    ldconfig
    ./configure --prefix=$AppDir --with-libevent=$AppBase && make && make install

    if [ $? -eq 0 ]; then
        echo "$AppName $Operate成功"
    else
        echo "$AppName $Operate失败" 
        exit 1
    fi
}

# 初始化
finit()
{
    echo "初始化 $AppName"

    id -gn $AppGroup &> /dev/null 
    [ $? -ne 0 ] && groupadd $AppGroup && echo "新建组：$AppGroup"

    id -un $AppUser &> /dev/null
    [ $? -ne 0 ] && useradd -s /bin/false -M -g $AppGroup $AppUser && echo "新建用户：$AppUser"
    [ $? -eq 0 ] && echo "s=ZNbw1mc@C" | passwd --stdin $AppUser &> /dev/null
}

# 启动
fstart()
{
    fpid

    if [ -n "$AppPid" ]; then
        echo "$AppName 正在运行"
    else
        $AppProg $Options && echo "启动 $AppName" || echo "$AppName 启动失败"
    fi
}

# 停止
fstop()
{
    fpid

    if [ -n "$AppPid" ]; then
        echo "$AppPid" | xargs kill -9 
        if [ $? -eq 0 ];then
            echo "停止 $AppName"
        else
            echo "$AppName 停止失败"
        fi
    else
        echo "$AppName 未启动"
    fi
}

# 重启
frestart()
{
    fpid
    [ -n "$AppPid" ] && fstop
    fstart
}

ScriptDir=$(dirname $0)
ScriptFile=$(basename $0)
case "$1" in
    'install'   ) finstall;;
    'update'    ) fupdate;;
    'reinstall' ) fremove && finstall;;
    'remove'    ) fremove;;
    'backup'    ) fbackup;;
    'init'      ) finit;;
    'start'     ) fstart;;
    'stop'      ) fstop;;
    'restart'   ) frestart;;
    'status'    ) fstatus;;
    *           )
    echo "$ScriptFile install              安装 $AppName"
    echo "$ScriptFile update               更新 $AppName"
    echo "$ScriptFile reinstall            重装 $AppName"
    echo "$ScriptFile remove               删除 $AppName"
    echo "$ScriptFile backup               备份 $AppName"
    echo "$ScriptFile init                 初始化 $AppName"
    echo "$ScriptFile start                启动 $AppName"
    echo "$ScriptFile stop                 停止 $AppName"
    echo "$ScriptFile restart              重启 $AppName"
    echo "$ScriptFile status               查询 $AppName 状态"
    ;;
esac
